/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package be.darling.scrt.utilities;

import be.darling.scrt.configuration.Configuration;
import be.darling.scrt.data.Condition;
import be.darling.scrt.data.Reader;
import be.darling.scrt.design.RandomizationTest;
import be.darling.scrt.generator.Generator;
import be.darling.scrt.statistics.Statistic;
import com.djahva.pax.Collector;
import com.djahva.pax.DOMReader;
import com.djahva.pax.DateReader;
import com.djahva.pax.HashMapReader;
import com.djahva.pax.MultiValueHashMap;
import com.djahva.pax.MultiValueHashMapReader;
import com.djahva.pax.PaxReader;
import java.util.Date;
import java.util.HashMap;
import org.w3c.dom.Node;

/**
 *
 * @author jingle
 */
public class InputFileProcessor {

    private RandomizationTest test;
    private Configuration configuration;

    public InputFileProcessor(RandomizationTest test, Configuration configuration)
    {
        this.test = test;
        this.configuration = configuration;
    }

    /**
     * Processes the inputfile of the <code>RandomizationTestobject</code> and just fills in the necessary info.
     *
     * The filling of the information of the reader, the generator and the statistics happens by
     * <code>InputFileInformationHandlers</code>
     */
    public void processInputFile()
    {
        PaxReader pax = new PaxReader();

        pax.addReader(new MultiValueHashMapReader(), "randomizationTest", "rootReader");
        pax.addReader(new DateReader("yyyy-MM-dd"), "randomizationTest/date", "dateReader")
                .setDestination( (Collector)pax.getReader("rootReader") );
        pax.addReader(new HashMapReader(), "randomizationTest/options", "optionsReader")
                .setDestination(  (Collector)pax.getReader("rootReader") );
        pax.addReader(new DOMReader(), "randomizationTest/options","DOMOptions")
                .setDestination( (Collector)pax.getReader("rootReader") );
        pax.getReader("rootReader").setDestination(new Collector(){
            public void put(String key, Object value) {
                MultiValueHashMap data = (MultiValueHashMap) value;

                test.setName( (String)data.get("name") );
                test.setDescription( (String)data.get("description") );
                test.setDate( (Date)data.get("dateReader") );

                for(int i=0; i<data.getCount("author"); i++)
                    test.getAuthors().add( (String)data.get("author", i) );

                test.setOptions( (HashMap)data.get("optionsReader") );
                test.setNodeOptions( (Node)data.get("DOMOptions") );

                System.out.println("\nTestName: " + test.getName());
                System.out.println("TestDescription: " + test.getDescription());
                System.out.println("Test Date: " + test.getDate());
                System.out.println("Test authors: " + test.getAuthors());
                System.out.println("Test options: " + test.getOptions());
                System.out.println("Test nodeOptions: " + test.getNodeOptions());
                System.out.println("Test conditions: " + test.getConditions());
                System.out.println("Test Reader: " + test.getReader());
                System.out.println("Test Design: " + test.getDesign());
                System.out.println("Test Statistics: " + test.getStatistics());
            }
        });


        pax.addReader(new MultiValueHashMapReader(), "data", "dataReader");
        pax.addReader(new HashMapReader(), "data/options", "optionsReader")
                .setDestination( (Collector)pax.getReader("dataReader") );
        pax.addReader(new DOMReader(), "data/options", "DOMOptions")
                .setDestination( (Collector)pax.getReader("dataReader") );
//        pax.addReader(new HashMapReader(), "data/conditions/condition", "conditionsReader")
//                .setDestination( (Collector)pax.getReader("dataReader") );
        pax.getReader("dataReader").setDestination(new Collector(){
            public void put(String key, Object value) {
                MultiValueHashMap data = (MultiValueHashMap) value;
                

                Reader reader = configuration.getDataReader( (String)data.get("type") );
                reader.setType((String)data.get("type"));
                reader.setOptions( (HashMap)data.get("optionsReader") );
                reader.setOptions( (Node)data.get("DOMOptions") );

                reader.init();

//                for(int i=0; i<data.getCount("conditionsReader"); i++)
//                {
//                    HashMap<String, String> condition = (HashMap)data.get("conditionsReader", i);
//                    test.addCondition( new Condition( condition.get("label"), condition.get("description") ) );
//                }
                test.setReader(reader);

                System.out.println("\nReader type: " +reader.getType());
                //System.out.println("Reader options: " + reader.getOptions());
                //System.out.println("Reader nodeOptions: " +reader.getNodeOptions());
                System.out.println("Readername: " + reader.getName() );
                System.out.println("Reader description: " + reader.getDescription());
                //System.out.println("Reader source: " +reader.getSource() );

            }
        });


        pax.addReader(new MultiValueHashMapReader(), "design", "designReader");
        pax.addReader(new HashMapReader(), "design/options", "optionsReader")
                .setDestination( (Collector)pax.getReader("designReader") );
        pax.addReader(new DOMReader(), "design/options", "DOMOptions")
                .setDestination( (Collector)pax.getReader("designReader") );
//        pax.addReader(new MultiValueHashMapReader(), "design/conditions", "conditionsReader")
//                .setDestination( (Collector)pax.getReader("designReader") );
        pax.addReader(new HashMapReader(), "design/conditions/condition", "conditionsReader")
                .setDestination( (Collector)pax.getReader("designReader") );
        pax.getReader("designReader").setDestination(new Collector(){
            public void put(String key, Object value) {
                MultiValueHashMap data = (MultiValueHashMap) value;

                Generator gen = configuration.getGenerator( (String)data.get("type") );
                gen.setType( (String)data.get("type") );
                gen.setOptions( (HashMap)data.get("optionsReader"));
                gen.setOptions( (Node)data.get("DOMOptions") );
                gen.init();

                for(int i=0; i<data.getCount("conditionsReader"); i++)
                {
                    HashMap<String, String> condition = (HashMap)data.get("conditionsReader", i);
                    gen.addCondition( new Condition( condition.get("label"), condition ) );
                }

                test.setDesign(gen);

                System.out.println("\nGenerator type: " +gen.getType());
                //System.out.println("Generator options: " + gen.getOptions());
                //System.out.println("Generator nodeOptions: " +gen.getNodeOptions());
                System.out.println("Generatorname: " + gen.getName() );
                System.out.println("Generator description: " + gen.getDescription());
            }
        });

        pax.addReader(new MultiValueHashMapReader(), "statistics", "statisticsReader");
        pax.addReader(new HashMapReader(), "statistics/statistic", "statisticReader")
                .setDestination( (Collector)pax.getReader("statisticsReader") );
        pax.addReader(new HashMapReader(), "statistics/statistic/options", "optionsReader")
                .setDestination( (Collector)pax.getReader("statisticReader") );
        pax.addReader(new DOMReader(), "statistics/statistic/options", "DOMOptions")
                .setDestination( (Collector)pax.getReader("statisticReader") );
        pax.getReader("statisticsReader").setDestination(new Collector(){
            public void put(String key, Object value) {
                MultiValueHashMap data = (MultiValueHashMap) value;

                for(int i=0; i<data.getCount("statisticReader"); i++)
                {
                    HashMap<String, Object> statistic = (HashMap)data.get("statisticReader", i);

                    Statistic stat = configuration.getStatistic( (String)statistic.get("type") );
                    stat.setName( (String)statistic.get("name"));
                    stat.setDescription( (String)statistic.get("description") );
                    stat.setType( (String)statistic.get("type") );
                    stat.setOptions( (HashMap)statistic.get("optionsReader"));
                    stat.setOptions( (Node)statistic.get("DOMOptions"));
                    stat.init();

                    test.addStatistic(stat);

                    System.out.println("\nStatistic type: " +stat.getType());
                    //System.out.println("Statistic options: " + stat.getOptions());
                    System.out.println("Statistic nodeOptions: " +stat.getNodeOptions());
                    System.out.println("Statistic conditions: " + stat.getConditions());
                    System.out.println("Statistic name: " + stat.getName() );
                    System.out.println("Statistic description: " + stat.getDescription());
                }
            }
        });

        pax.parse(test.getFile());

    }
}
